package com.lab.jumper;

import com.lab.app.BaseActivity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by ak
 * Activity管理器
 */
public class ActivityManager<T extends BaseActivity> {

    private static ActivityManager sInstance;

    public static synchronized ActivityManager getInstance() {
        if (null == sInstance)
            sInstance = new ActivityManager();
        return sInstance;
    }

    private Stack<WeakReference<T>> mActivityStack;

    private ActivityManager() {
        ensureStack();
    }

    public void addActivity(T activity) {
        if (null != getWeakActivityIfExist(activity))
            return;
        WeakReference<T> weakActivity = new WeakReference<T>(activity);
        mActivityStack.add(weakActivity);
//        AppLog.d("ActivityManager addActivity = " + ((Object) activity).getClass().getSimpleName());
    }

    public void removeActivity(T activity) {
        WeakReference weakActivity = getWeakActivityIfExist(activity);
        if (null == weakActivity)
            return;
        mActivityStack.remove(weakActivity);
//        AppLog.d("ActivityManager removeActivity = " + ((Object) activity).getClass().getSimpleName());
    }

    public void clearAllActivity() {
        for (WeakReference weakActivity : mActivityStack) {
            if (null != weakActivity) {
                T activity = (T) weakActivity.get();
                if (null != activity) {
                    mActivityStack.remove(weakActivity);
                    activity.finish();
                }
            }
        }
        mActivityStack.clear();
    }

    public List<T> getAllActivity() {
        List<T> listActivity = new ArrayList<T>();
        if (null != mActivityStack) {
            for (WeakReference<T> activityWeakReference : mActivityStack) {
                if (null != activityWeakReference) {
                    T activity = activityWeakReference.get();
                    if (null != activity) {
                        listActivity.add(activity);
                    }
                }
            }
        }
        return listActivity;
    }

    public T getTopActivity() {
        if (null != mActivityStack) {
            WeakReference<T> activityWeakReference = mActivityStack.peek();
            if (null != activityWeakReference) {
                T activity = activityWeakReference.get();
                return activity;
            }
        }
        return null;
    }

    private WeakReference<T> getWeakActivityIfExist(T activity) {
        if (null == activity || mActivityStack.isEmpty())
            return null;
        for (WeakReference weakActivity : mActivityStack) {
            if (null != weakActivity) {
                T existActivity = (T) weakActivity.get();
                if (null != existActivity && existActivity == activity)
                    return weakActivity;
                continue;
            }
        }
        return null;
    }

    private void ensureStack() {
        if (null == mActivityStack)
            mActivityStack = new Stack<WeakReference<T>>();
    }

}